home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / Tool Chest / Development Platforms / AppsToGo / AppsToGo.src / DTS.Draw / TLineObj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-18  |  7.9 KB  |  314 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        TLineObj.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19. /* See the files "=How to write your app" and "=Using TreeObj.c" for information
  20. ** on this function. */
  21.  
  22. /* This file implements the messages for the round-rect object.  Many of the messages
  23. ** can be handled by the rect object, as they deal with a rect structure.  Only
  24. ** a few of them are round-rect-specific. */
  25.  
  26. /* It would seem that you would want a custom hit-test message handler here, but
  27. ** the rect object first checks to see if the hit is within the bounding box of
  28. ** the object, and if so, it then calls the object to return the region.  This
  29. ** allows the rect object to generically handle hit-testing. */
  30.  
  31.  
  32.  
  33. /*****************************************************************************/
  34.  
  35.  
  36.  
  37. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  38. #include "App.protos.h"        /* Get the prototypes for the application.        */
  39.  
  40. #ifndef __OSEVENTS__
  41. #include <OSEvents.h>
  42. #endif
  43.  
  44. #ifndef __OSUTILS__
  45. #include <OSUtils.h>
  46. #endif
  47.  
  48. #ifndef __QUICKDRAW__
  49. #include <Quickdraw.h>
  50. #endif
  51.  
  52. #ifndef __STRING__
  53. #include <String.h>
  54. #endif
  55.  
  56. #ifndef __TREEOBJ2__
  57. #include "TreeObj2.h"
  58. #endif
  59.  
  60. #ifndef __UTILITIES__
  61. #include "Utilities.h"
  62. #endif
  63.  
  64.  
  65.  
  66. static OSErr    LineLayerProc(LayerObj theLayer, short message);
  67.  
  68.  
  69.  
  70. /*****************************************************************************/
  71.  
  72.  
  73.  
  74. #pragma segment DrawObjects
  75. long    TLineObj(TreeObjHndl hndl, short message, long data)
  76. {
  77.     Rect        rct, drct, grabber;
  78.     RgnHandle    rgn;
  79.     short        orn, h, w, i;
  80.     LayerObj    lineLayer;
  81.     Rect        *rptr;
  82.     Boolean        change;
  83.     ClickInfo    *click;
  84.     RGBColor    rgb, oldRgb;
  85. #if VH_VERSION
  86.     char        *cptr;
  87. #endif
  88.  
  89.     switch (message) {
  90.         case INITMESSAGE:
  91.         case FREEMESSAGE:
  92.         case COPYMESSAGE:
  93.         case UNDOMESSAGE:
  94.         case CONVERTMESSAGE:
  95.         case FREADMESSAGE:
  96.         case FWRITEMESSAGE:
  97.         case HREADMESSAGE:
  98.         case HWRITEMESSAGE:
  99.         case HITTESTMESSAGE:
  100.         case GETOBJRECTMESSAGE:
  101.         case SETOBJRECTMESSAGE:
  102.         case SECTOBJRECTMESSAGE:
  103.         case CLICKMESSAGE:
  104.         case KEYMESSAGE:
  105.         case SETSELECTMESSAGE:
  106.         case GETSELECTMESSAGE:
  107.         case COMPAREMESSAGE:
  108.             return(TRectObj(hndl, message, data));
  109.             break;
  110.  
  111.         case GETBBOXMESSAGE:
  112.             TRectObj(hndl, message, data);
  113.             h = (mDerefCommon(hndl)->penHeight / 2) + 1;
  114.             w = (mDerefCommon(hndl)->penWidth  / 2) + 1;
  115.             rptr = (Rect *)data;
  116.             rptr->left   -= w;
  117.             rptr->right  += w;
  118.             rptr->top    -= h;
  119.             rptr->bottom += h;
  120.             break;
  121.  
  122.         case GETRGNMESSAGE:
  123.             rgn = NewRgn();
  124.             NewLayer(&lineLayer, nil, LineLayerProc, nil, 0, (long)hndl);
  125.             if (lineLayer) {
  126.                 SetLayerWorld(lineLayer);
  127.                 TLineObj(hndl, DRAWMESSAGE, DRAWMASK);
  128.                 BitMapToRegion(rgn, (BitMapPtr)(*((CGrafPtr)(*lineLayer)->layerPort)->portPixMap));
  129.                 ResetLayerWorld(lineLayer);
  130.                 DisposeLayer(lineLayer);
  131.                 InsetRgn(rgn, -1, -1);
  132.             }
  133.             return((long)rgn);
  134.             break;
  135.  
  136.         case DRAWMESSAGE:
  137.             rct  = mDerefLine(hndl)->rect;
  138.             orn  = mDerefLine(hndl)->flip;
  139.             drct = rct;
  140.             switch (orn) {
  141.                 case 1:
  142.                     drct.top    = rct.bottom;
  143.                     drct.bottom = rct.top;
  144.                     break;
  145.                 case 2:
  146.                     drct.left  = rct.right;
  147.                     drct.right = rct.left;
  148.                     break;
  149.                 case 3:
  150.                     drct.top    = rct.bottom;
  151.                     drct.bottom = rct.top;
  152.                     drct.left   = rct.right;
  153.                     drct.right  = rct.left;
  154.                     break;
  155.             }
  156.             h = mDerefCommon(hndl)->penHeight;
  157.             w = mDerefCommon(hndl)->penWidth;
  158.             PenSize(w, h);
  159.             switch (data) {
  160.                 case DRAWOBJ:
  161.                     if (gQDVersion) {
  162.                         GetForeColor(&oldRgb);
  163.                         rgb = mDerefLine(hndl)->borderColor;
  164.                         RGBForeColor(&rgb);
  165.                     }
  166.                     MoveTo(drct.left - (w / 2), drct.top - (h / 2));
  167.                     LineTo(drct.right - (w / 2), drct.bottom - (h / 2));
  168.                     if (gQDVersion)
  169.                         RGBForeColor(&oldRgb);
  170.                     break;
  171.                 case ERASEOBJ:
  172.                     break;
  173.                 case DRAWSELECT:
  174.                     if (mDerefCommon(hndl)->selected) {
  175.                         for (i = 0; i < 2; ++i) {
  176.                             grabber.top  = i ? drct.top  : drct.bottom;
  177.                             grabber.left = i ? drct.left : drct.right;
  178.                             grabber.bottom = (grabber.top  -= 3) + 6;
  179.                             grabber.right  = (grabber.left -= 3) + 6;
  180.                             InvertRect(&grabber);
  181.                         }
  182.                     }
  183.                     break;
  184.                 case DRAWGHOST:
  185.                     PenMode(patXor);
  186.                     TLineObj(hndl, DRAWMESSAGE, DRAWOBJ);
  187.                     break;
  188.                 case DRAWMASK:
  189.                     ForeColor(blackColor);
  190.                     MoveTo(drct.left - (w / 2), drct.top - (h / 2));
  191.                     LineTo(drct.right - (w / 2), drct.bottom - (h / 2));
  192.                     break;
  193.             }
  194.             PenNormal();
  195.             break;
  196.  
  197.         case PRINTMESSAGE:
  198.             TLineObj(hndl, DRAWMESSAGE, DRAWOBJ);
  199.             break;
  200.  
  201. #if VH_VERSION
  202.         case VHMESSAGE:
  203.             cptr = ((VHFormatDataPtr)data)->data;
  204.             ccatchr(cptr, 13, 2);
  205.             ccat   (cptr, "$10: TRectObj:");
  206.             ccatchr(cptr, 13, 1);
  207.             ccat   (cptr, "  $00: selected     = ");
  208.             ccatdec(cptr, mDerefLine(hndl)->selected);
  209.             ccatchr(cptr, 13, 1);
  210.             rct = mDerefLine(hndl)->rect;
  211.             ccat   (cptr, "  $02: rect         = ($");
  212.             ccathex(cptr, 0, 4, 4, rct.top);
  213.             ccat   (cptr, ",$");
  214.             ccathex(cptr, 0, 4, 4, rct.left);
  215.             ccat   (cptr, ",$");
  216.             ccathex(cptr, 0, 4, 4, rct.bottom);
  217.             ccat   (cptr, ",$");
  218.             ccathex(cptr, 0, 4, 4, rct.right);
  219.             ccat   (cptr, ")");
  220.             ccatchr(cptr, 13, 1);
  221.             ccat   (cptr, "  $0A: penHeight    = ");
  222.             ccatdec(cptr, mDerefLine(hndl)->penHeight);
  223.             ccatchr(cptr, 13, 1);
  224.             ccat   (cptr, "  $0A: penWidth     = ");
  225.             ccatdec(cptr, mDerefLine(hndl)->penWidth);
  226.             ccatchr(cptr, 13, 1);
  227.             ccat   (cptr, "  $0E: borderColor  = ($");
  228.             ccathex(cptr, 0, 4, 4, mDerefLine(hndl)->borderColor.red);
  229.             ccat   (cptr, ",$");
  230.             ccathex(cptr, 0, 4, 4, mDerefLine(hndl)->borderColor.green);
  231.             ccat   (cptr, ",$");
  232.             ccathex(cptr, 0, 4, 4, mDerefLine(hndl)->borderColor.blue);
  233.             ccat   (cptr, ")");
  234.             ccatchr(cptr, 13, 1);
  235.             ccat   (cptr, "  $14: content      = ($");
  236.             ccatdec(cptr, mDerefLine(hndl)->content);
  237.             ccatchr(cptr, 13, 1);
  238.             ccat   (cptr, "  $16: contentColor = ($");
  239.             ccathex(cptr, 0, 4, 4, mDerefLine(hndl)->contentColor.red);
  240.             ccat   (cptr, ",$");
  241.             ccathex(cptr, 0, 4, 4, mDerefLine(hndl)->contentColor.green);
  242.             ccat   (cptr, ",$");
  243.             ccathex(cptr, 0, 4, 4, mDerefLine(hndl)->contentColor.blue);
  244.             ccat   (cptr, ")");
  245.             ccatchr(cptr, 13, 1);
  246.             ccat   (cptr, "  $1A: flip         = ($");
  247.             ccatdec(cptr, mDerefLine(hndl)->flip);
  248.             return(true);
  249.             break;
  250. #endif
  251.  
  252.         case SIZEMESSAGE:
  253.             if (change = TRectObj(hndl, message, data)) {
  254.                 click = (ClickInfo *)data;
  255.                 mDerefLine(hndl)->flip = click->newFlip;
  256.             }
  257.             return(change);
  258.             break;
  259.  
  260.         default:
  261.             break;
  262.     }
  263.  
  264.     return(noErr);
  265. }
  266.  
  267.  
  268.  
  269. /*****************************************************************************/
  270.  
  271.  
  272.  
  273. static OSErr    LineLayerProc(LayerObj theLayer, short message)
  274. {
  275.     OSErr        err;
  276.     TreeObjHndl    line;
  277.     Rect        rct;
  278.     CGrafPtr    keepPort;
  279.     GDHandle    keepGDevice;
  280.     GWorldPtr    layerWorld;
  281.  
  282.     switch (message) {
  283.         case kLayerInit:
  284.             err = noErr;
  285.             if (theLayer) {
  286.                 if (!(*theLayer)->layerPort) {
  287.                     line = (TreeObjHndl)(*theLayer)->layerData;
  288.                     TLineObj(line, GETBBOXMESSAGE, (long)&rct);
  289.                     GetGWorld(&keepPort, &keepGDevice);        /* Keep the GWorld. */
  290.                     err = NewGWorld(&layerWorld, 1, &rct, nil, nil, 0);
  291.                     if (err == noErr) {
  292.                         (*theLayer)->layerOwnsPort = true;
  293.                         SetPort((*theLayer)->layerPort = (GrafPtr)layerWorld);
  294.                         SetOrigin(rct.left, rct.top);
  295.                         EraseRect(&rct);
  296.                     }
  297.                     SetGWorld(keepPort, keepGDevice);        /* Restore the kept GWorld. */
  298.                 }
  299.             }
  300.             else err = paramErr;
  301.             break;
  302.  
  303.         default:
  304.             err = DefaultLayerProc(theLayer, message);
  305.                 /* Default behavior for everything else. */
  306.             break;
  307.     }
  308.  
  309.     return(err);
  310. }
  311.  
  312.  
  313.  
  314.